package com.edu.framework.websocket.handler;

import com.alibaba.fastjson2.JSONArray;
import com.edu.common.utils.StringUtils;
import com.edu.framework.websocket.WebSocketHolder;
import com.edu.framework.websocket.message.base.Response;
import com.edu.framework.websocket.message.info.InfoRequest;
import com.edu.framework.websocket.service.BaseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.Map;

@Component
public class InfoRequestHandler implements IHandler<InfoRequest> {

    @Autowired
    private BaseInfoService baseInfoService;

    @Override
    public void execute(WebSocketSession session, InfoRequest request) {
        if (request == null) {
            return;
        }
        Map<String, String> data = request.getData();
        String type = data.get("type");

//        if(StringUtils.equals("bindInfo",type)){
//            String code = WebSocketHolder.getCodeBySession(session);
//            Response bindInfo = baseInfoService.getBindInfo(code);
//            try {
//                WebSocketHolder.send(session,bindInfo);
//            } catch (IOException e) {
//                throw new RuntimeException(e);
//            }
//            return;
//        }
        JSONArray infoByType = baseInfoService.getInfoByType(type);
        Response<JSONArray> response = Response.ok(InfoRequest.TYPE, type, infoByType);
        try {
            WebSocketHolder.send(session, response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getType() {
        return InfoRequest.TYPE;
    }
}
